﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;

namespace Zove.Desktop.Tools {
    public partial class PieChartWindow : Form {

        private string[] parts;

        private double[] p;

        private string comment;

        public PieChartWindow() {
            InitializeComponent();

            this.Load += new EventHandler(PieChartWindow_Load);
        }

        public PieChartWindow(string title, string[] part, double[] p, string comment)
            : this() {
            this.Text = title;
            this.parts = part;
            this.p = p;
            this.comment = comment;
        }

        public void PieChartWindow_Load(object sender, EventArgs e) {
            this.CreateChart(this.zedGraphControl1);
        }

        // Call this method from the Form_Load method, passing your ZedGraphControl
        public void CreateChart(ZedGraphControl zgc) {
            GraphPane myPane = zgc.GraphPane;

            // Set the GraphPane title
            myPane.Title.Text = this.Text;
            myPane.Title.FontSpec.IsItalic = true;
            myPane.Title.FontSpec.Size = 24f;
            myPane.Title.FontSpec.Family = "Times New Roman";

            // Fill the pane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.Goldenrod, 45.0f);
            // No fill for the chart background
            myPane.Chart.Fill.Type = FillType.None;

            // Set the legend to an arbitrary location
            myPane.Legend.Position = LegendPos.Float;
            myPane.Legend.Location = new Location(
                0.95f, 0.15f, CoordType.PaneFraction,
                           AlignH.Right, AlignV.Top);
            myPane.Legend.FontSpec.Size = 10f;
            myPane.Legend.IsHStack = false;

            // Add some pie slices
            for(int index = 0; index < p.Length; index++) {
                PieItem segment1 = myPane.AddPieSlice(
                    p[index], Color.Navy, Color.White, 45f, 0, parts[index]);
            }

            // Sum up the pie values                                                               
            CurveList curves = myPane.CurveList;
            double total = 0;
            for (int x = 0; x < curves.Count; x++)
                total += ((PieItem)curves[x]).Value;

            // Make a text label to highlight the total value
            TextObj text = new TextObj(comment,
                           0.18F, 0.40F, CoordType.PaneFraction);
            text.Location.AlignH = AlignH.Center;
            text.Location.AlignV = AlignV.Bottom;
            text.FontSpec.Border.IsVisible = false;
            text.FontSpec.Fill = new Fill(Color.White, Color.FromArgb(255, 100, 100), 45F);
            text.FontSpec.StringAlignment = StringAlignment.Center;
            myPane.GraphObjList.Add(text);

            // Create a drop shadow for the total value text item
            TextObj text2 = new TextObj(text);
            text2.FontSpec.Fill = new Fill(Color.Black);
            text2.Location.X += 0.008f;
            text2.Location.Y += 0.01f;
            myPane.GraphObjList.Add(text2);

            // Calculate the Axis Scale Ranges
            zgc.AxisChange();

        }
    }
}
